package playlist;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import config.ConfigFile;

public class ComposerConfig {

	// Playlist types, oprdered by types names
	private static Map<String, PlaylistType> config;

	private static void initConfig() throws Throwable {

		config = new HashMap<String, PlaylistType>();
		String composerConfigFile = ConfigFile
				.getProperty("composerConfigFile");
		BufferedReader br = new BufferedReader(new FileReader(
				composerConfigFile));
		String line = null;
		PlaylistType currentPlaylistType = null;
		String name = null;

		while ((line = br.readLine()) != null) {
			if (line.startsWith("name:")) {
				name = line.substring(5);
			} else if (line.startsWith("\tstart:")) {
				currentPlaylistType = new PlaylistType(name, line.substring(7));
				config.put(name, currentPlaylistType);
			} else if (line.startsWith("\ttransition:")) {
				String start = line.split(":")[1];
				String[] transitions = line.split(":")[2].split(",");
				currentPlaylistType.addTransition(start, transitions);
			}
		}
		br.close();
	}

	public static String getStartType(String typeName) throws Throwable {
		if (config == null) {
			initConfig();
		}
		return config.get(typeName).getStart();
	}

	public static String getNewTransition(String typeName) throws Throwable {
		if (config == null) {
			initConfig();
		}
		return config.get(typeName).getNewTransition();
	}
}
